package servlet.car;

import domain.Car;
import domain.Page;
import service.GoodsService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;


@WebServlet("/GotoCarServlet")
public class GotoCarServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        if(session.getAttribute("zhi") == null) {
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().print("<script language='javascript'>  \n" +
                    "    alert('您还没有登录，请返回登录界面进行登录！！！');  \n" +
                    "    window.location.href = 'index.jsp';  \n" +
                    "</script>");
        }
        int user_id = (int) request.getSession().getAttribute("zhi");
        String current = request.getParameter("current");
        int curPage=(current==null?1:Integer.parseInt(current));
        GoodsService goodsService = new GoodsService();
        List<Car> goods = goodsService.findCarsByUserid(user_id);
        request.setAttribute("goods", goods);
        List<Page> pages = goodsService.getPages(user_id);
        request.setAttribute("pages", pages);
        request.setAttribute("current", curPage);
        request.setAttribute("total",pages.size());
        request.setAttribute("user_id",user_id);
        request.getRequestDispatcher("car.jsp").forward(request,response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
